<?php
namespace Admin\Model;
use Think\Model;

class CustomerModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('Name','require','请输入客户名称',Model::MUST_VALIDATE),
        array('Contact','require','请输入联系人姓名',Model::MUST_VALIDATE),
        array('Address','require','请输入地址',Model::MUST_VALIDATE),
        array('Phone','number','请输入手机号码',Model::MUST_VALIDATE)
    );
	
	protected $_auto = array(
    	array('AddTime', 'getInputTime',3,'callback')
  	);
  	
  	public function getInputTime(){
		return date('Y-m-d H:i:s');
	}
	
	public function Myupadd(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->add();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Mydelet(){
		$dbName = $this->getTableName();
		$ID = I("ID");
		$sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
		$res = $this->execute($sql);
		if($res){
			$this->results['state'] = 1;
			$this->results['msg'] = "删除成功！";
			$this->results['id'] = $res;
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "删除错误！";
		}
		return $this->results;
	}
	
	public function Myshow($parmArr){
		return $this->field("*")->WHERE($parmArr)->find();
	}
	
	public function MyshowList(){
		$where = "";
		$keyword = I('keyword');
        if(!empty($keyword)){
            $where['Name|Contact|Address|Phone'] = array('like','%'.$keyword.'%');
        }
        $where['IsDel'] = 0;
		$return = array();
		$count = $this->where($where)->count();
	    $page = $this->Mypage($count, 8);
	    $fields = "a.*";
	    $join = "";
		$res = $this->alias('a')->join($join)->where($where)->field($fields)->limit($page->firstRow.','.$page->listRows)->select();
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
	
}

?>				